Search K
Appearance
Appearance
服务编排是一种按照特定业务规则来集中管理容器的方法。在微服务架构中,应用系统通常包含多个微服务,而每个微服务可能有多个部署实例。
微服务架构中的挑战:
服务编排的解决方案:
服务编排所需的任务:
docker-compose up 启动整个应用。# docker-compose.yml
version: "3"
services:
webapp:
build:
context: .
dockerfile: Dockerfile.webapp
ports:
- "8080:80"
database:
image: postgres:latest
environment:
POSTGRES_DB: mydatabase
POSTGRES_USER: myuser
POSTGRES_PASSWORD: mypassword# Linuxbrew
brew install docker-compose docker-completion# windows
scoop install main/docker-compose# linux Compose standalone
curl -SL https://github.com/docker/compose/releases/download/v2.23.3/docker-compose-linux-x86_64 -o /usr/local/bin/docker-compose
sudo ln -s /usr/local/bin/docker-compose /usr/bin/docker-compose
# Windows Server
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
Start-BitsTransfer -Source "https://github.com/docker/compose/releases/download/v2.23.3/docker-compose-windows-x86_64.exe" -Destination $Env:ProgramFiles\Docker\docker-compose.exe❯ docker-compose version
Docker Compose version v2.23.3# Linuxbrew
brew uninstall docker-compose docker-completion
# windows
scoop uninstall docker-compose
# 查看 Compose standalone 安装位置
docker info --format '{{range .ClientInfo.Plugins}}{{if eq .Name "compose"}}{{.Path}}{{end}}{{end}}'
# 卸载 docker-compose
sudo rm /usr/local/bin/docker-compose -f定义两个服务:一个用于 Nginx 作为反向代理服务器,另一个用于 Spring Boot 应用。
创建 Spring Boot 应用 Dockerfile (Dockerfile.springboot)
# 使用 OpenJDK 11 作为基础镜像
FROM openjdk:11
# 设置工作目录
WORKDIR /app
# 复制构建好的 Spring Boot JAR 文件到容器
COPY target/springboot-hello-0.0.1-SNAPSHOT.jar /app/app.jar
# 暴露应用运行的端口
EXPOSE 8080
# 定义启动命令
CMD ["java", "-jar", "app.jar"]创建 Docker Compose 文件 (docker-compose.yml)
version: "3"
services:
nginx:
image: nginx:latest
ports:
- "80:80"
volumes:
- ./nginx.conf:/etc/nginx/nginx.conf
depends_on:
- springboot
springboot:
build:
context: .
dockerfile: Dockerfile.springboot
ports:
- "8080:8080"创建 Nginx 配置文件 (nginx.conf)
events {}
http {
server {
listen 80;
location / {
proxy_pass http://springboot:8080;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
}
}查看项目结构。
❯ tree
├── Dockerfile.springboot
├── docker-compose.yml
├── nginx.conf
└── target
└── springboot-hello-0.0.1-SNAPSHOT.jar构建和运行。
❯ docker-compose up -d --build测试访问。
❯ curl localhost/hello